<template>
  <el-row class="box" justify="center" align="middle" style="height: 100%">
    <el-col :span="14">
      <el-row justify="center" align="middle" class="login-box">
        <el-col :span="24" >
          <el-form class="form" label-width="120px">
            <el-row>
              <el-col :span="24" class="title">无秋管理系统</el-col>
            </el-row>
            <el-row>
              <el-input :disabled = "btnLoad" @keyup.enter.native="onSubmit"
                        v-model="form.username" placeholder="请输入用户名" prefix-icon="User"/>
            </el-row>
            <el-row>
              <el-input :disabled = "btnLoad" @keyup.enter.native="onSubmit"
                        v-model="form.password" placeholder="请输入用户密码" show-password prefix-icon="Lock"/>
            </el-row>
            <el-row v-show="captchaNeed">
              <el-input :disabled = "btnLoad" @keyup.enter.native="onSubmit"
                        v-model="form.captcha" placeholder="请输入验证码" style="flex: 1" prefix-icon="Aim"/>
              <captcha ref="captcha" height="50" v-model:token="form.token" style="margin-left: 1rem"></captcha>
            </el-row>
            <el-row justify="center">
              <el-button :loading="btnLoad" @click="onSubmit" style="width: 100%;">登录</el-button>
            </el-row>
          </el-form>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import {ref, unref, getCurrentInstance, watch, reactive, onMounted} from "vue";
import {useRouter, useRoute} from 'vue-router'
import {ElMessage, ElMessageBox} from 'element-plus';

import userApi from "@/api/sys/user";
import tool from "@/utils/tool";

export default {
  name: "login",
  components: {
  },
  setup(props, content) {
    const router = useRouter()
    const captcha = ref(null)

    let data = {
      form: ref({}),
      btnLoad : ref(false),
      captchaNeed: ref(false),
    }
    //监听
    watch(() => [props.info], ([newInfo], [oldInfo]) => {
      // if (newInfo){
      //   Object.assign(data.form, newInfo)
      // }
    })

    onMounted(() => {
    })

    let methods = {
      onSubmit() {
        data.btnLoad.value = true
        data.form.value.loginname =  data.form.value.username
        let param = tool.changeJson(data.form.value)

        userApi.login(param).then(res => {
          if (res.success) {
            ElMessage.success(res.msg);
            router.push({
              path: '/',
            })
          } else {
            if (res.msg === "验证码不能为空"){
              data.captchaNeed.value = true
            }else{
              ElMessage.warning(res.msg);
              captcha.value.getCaptcha()
            }
          }
        }).catch((e) => {
          ElMessage.error(e);
        }).finally(()=> {
          data.btnLoad.value = false
        })
      },
    }

    return {
      router,
      captcha,
      ...data,
      ...methods,
    }
  }
}
</script>

<style lang="scss" scoped>
.box{
  background-color: var(--el-bg-color);
}
.form {
  padding: 1rem 2rem;

  .el-row {
    margin: 1.5rem 0;
  }
  .el-button, .el-input{
    height: 50px;
  }
}
.login-box {
  height: 100%;
  border-radius: 1rem;
  box-shadow: 0rem 0rem 1.5rem var(--el-color-info);
  background-color: transparent;
}
.title{
  text-align: center;
  font-size: 1.5rem;
}

</style>